package org.nidy.erp.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

@Slf4j

public class WeChatBotSender {
    public static void sendMessage(String BotUrl,String jsonBody) {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 发送POST请求
            HttpPost httpPost = new HttpPost(BotUrl);
            httpPost.setHeader("Content-Type", "application/json");
            httpPost.setEntity(new StringEntity(jsonBody, "UTF-8"));

            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == 200) {
                    System.out.println("消息发送成功");
                    log.info("消息发送成功:机器人地址：{}，消息内容：{}", BotUrl,jsonBody);
                } else {
                    System.out.println("发送失败，状态码：" + statusCode+"，机器人地址："+BotUrl+"，消息内容："+jsonBody);
                    log.error("发送失败，状态码：{}，机器人地址：{}，消息内容：{}", statusCode,BotUrl,jsonBody);
                }
            }
        } catch (Exception e) {
           e.printStackTrace();
           log.error("发送失败，机器人地址:{}，消息内容:{}", BotUrl,jsonBody);
        }
    }
}